package com.zhuoyue.biz.core.command.cmd.common.faa;

import com.zhuoyue.biz.core.api.domain.DeviceAndGatewayDO;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;
import com.zhuoyue.biz.core.command.cmd.common.faa.base.BaseCmd;
import com.zhuoyue.biz.core.command.cmd.common.faa.base.IndexWay;
import com.zhuoyue.biz.core.command.cmd.common.faa.base.ModeEnum;
import com.zhuoyue.biz.core.command.cmd.common.faa.base.TimeEnum;
import com.zhuoyue.common.constant.ControlType;
import com.zhuoyue.common.constant.DataBase;
import com.zhuoyue.common.exception.ErrorType;
import com.zhuoyue.common.exception.ZyServerException;
import com.zhuoyue.common.utils.CmdUtils;

/**
 * @ClassName: ClothesHanger0Faa0C02
 * @Description: 晾衣架
 * @author wjming
 * @Date 2021-07-15 9:53
 */
public class ClothesHanger0Faa0C02 extends BaseCmd {
    private static final String OPEN = "open";
    private static final String SWITCH_STATE = "switchState";
    private static final String MODE_STATE = "switchState";
    private static final String TIME = "time";
    private static final String DELAY_TIME = "delayTime";

    public static String control(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {
        String cmd = "";
        switch (controlReq.getControlType()) {
            //上升
            case ControlType.RISE_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, open());
                break;
            //下降
            case ControlType.DESCEND_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, close());
                break;
            //暂停
            case ControlType.PAUSE_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, pause());
                break;
            //延时停
            case ControlType.TIMING_PAUSE:
                int delayTime = controlReq.getActions().get(DELAY_TIME) == null ? 0 : (int) controlReq.getActions().get(DELAY_TIME);
                cmd = getControlCmd(deviceAndGatewayDO, pauseConfirmation(CmdUtils.getHexString(delayTime)));
                break;
            //延时上升
            case ControlType.TIMING_RISE_REQUEST:
                int delayTime1 = controlReq.getActions().get(DELAY_TIME) == null ? 0 : (int) controlReq.getActions().get(DELAY_TIME);
                cmd = getControlCmd(deviceAndGatewayDO, openConfirmation(CmdUtils.getHexString(delayTime1)));
                break;
            //延时下降
            case ControlType.TIMING_DESCEND_REQUEST:
                int delayTime2 = controlReq.getActions().get(DELAY_TIME) == null ? 0 : (int) controlReq.getActions().get(DELAY_TIME);
                cmd = getControlCmd(deviceAndGatewayDO, closeConfirmation(CmdUtils.getHexString(delayTime2)));
                break;
            //照明灯打开
            case ControlType.TURN_ON_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, lightOpen());
                break;
            //照明灯关闭
            case ControlType.TURN_OFF_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, lightClose());
                break;
            //照明灯延时打开
            case ControlType.TIMING_TURN_ON_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, lightOpenConfirmation());
                break;
            //照明灯延时关闭
            case ControlType.TIMING_TURN_OFF_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, lightCloseConfirmation());
                break;
            //模式设置
            case ControlType.SET_MODE_REQUEST:
                if (controlReq.getActions().get(MODE_STATE) == null || controlReq.getActions().get(SWITCH_STATE) == null) {
                    throw new ZyServerException(ErrorType.INVALID_PARAMS.getCode(), "参数错误，请检查对应参数是否正确");
                }
                String switchState = (String) controlReq.getActions().get(SWITCH_STATE);
                String modeState = (String) controlReq.getActions().get(MODE_STATE);
                int time = 0;
                try {
                    time = (int) controlReq.getActions().get(TIME);
                } catch (Exception e) {
                    System.out.printf("取消模式,时间为空");
                }


                if (OPEN.equals(switchState) && controlReq.getActions().get(TIME) == null) {
                    throw new ZyServerException(ErrorType.INVALID_PARAMS.getCode(), "配置时间不能为空");
                }
                switch (modeState) {
                    //烘干模式
                    case ControlType.SetMode.DRYING:
                        cmd = getControlCmd(deviceAndGatewayDO, dryingMode(switchState, time));
                        break;
                    //风干模式
                    case ControlType.SetMode.AIR_DRY:
                        cmd = getControlCmd(deviceAndGatewayDO, airDryMode(switchState, time));
                        break;
                    //消毒模式
                    case ControlType.SetMode.DISINFECT:
                        cmd = getControlCmd(deviceAndGatewayDO, disinfectMode(switchState, time));
                        break;
                    //负离子模式
                    case ControlType.SetMode.NAI:
                        cmd = getControlCmd(deviceAndGatewayDO, naiMode(switchState, time));
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
        return cmd;
    }

    private static String naiMode(String switchState, int time) {
        if (OPEN.equals(switchState)) {
            return IndexWay.getEnumType("3d") + ModeEnum.getEnumCmd(ControlType.SetMode.NAI) + "01" + TimeEnum.getTimeEnum(time);
        } else {
            return IndexWay.getEnumType("3") + ModeEnum.getEnumCmd(ControlType.SetMode.NAI) + "02";
        }
    }

    private static String disinfectMode(String switchState, int time) {
        if (OPEN.equals(switchState)) {
            return IndexWay.getEnumType("3d") + ModeEnum.getEnumCmd(ControlType.SetMode.DISINFECT) + "01" + TimeEnum.getTimeEnum(time);
        } else {
            return IndexWay.getEnumType("3") + ModeEnum.getEnumCmd(ControlType.SetMode.DISINFECT) + "02";
        }
    }

    private static String airDryMode(String switchState, int time) {
        if (OPEN.equals(switchState)) {
            return IndexWay.getEnumType("3d") + ModeEnum.getEnumCmd(ControlType.SetMode.AIR_DRY) + "01" + TimeEnum.getTimeEnum(time);
        } else {
            return IndexWay.getEnumType("3") + ModeEnum.getEnumCmd(ControlType.SetMode.AIR_DRY) + "02";
        }
    }

    private static String dryingMode(String switchState, int time) {
        if (OPEN.equals(switchState)) {
            return IndexWay.getEnumType("3d") + ModeEnum.getEnumCmd(ControlType.SetMode.DRYING) + "01" + TimeEnum.getTimeEnum(time);
        } else {
            return IndexWay.getEnumType("3") + ModeEnum.getEnumCmd(ControlType.SetMode.DRYING) + "02";
        }

    }


    /**
     * 灯打开
     *
     * @return
     */
    private static String lightOpen() {
        return IndexWay.getEnumType("2") + DataBase.DIRECT + "01";
    }

    /**
     * 灯关闭
     *
     * @return
     */
    private static String lightClose() {
        return IndexWay.getEnumType("2") + DataBase.DIRECT + "02";
    }

    /**
     * 灯延时打开
     *
     * @return
     */
    private static String lightOpenConfirmation() {
        return IndexWay.getEnumType("2d") + DataBase.DIRECT + "01";
    }

    /**
     * 灯延时关闭
     *
     * @return
     */
    private static String lightCloseConfirmation() {
        return IndexWay.getEnumType("2d") + DataBase.DIRECT + "02";
    }


}
